<div id="panel-button" class="row">
	<div class="col-md-12">
		<div class="panel panel-default">
			<div class="panel-body text-right">
				{{Form::button(trans('form.save'), array('class' => 'btn btn-primary', 'id' => 'submit'))}}
				@if(isset($customer->id))
				{{Form::button(trans('form.delete'), array('class' => 'btn btn-danger', 'id' => 'delete', 'data-id' => $customer->id))}}
				@endif
				{{Form::button(trans('form.back'), array('class' => 'btn btn-default', 'id' => 'cancel'))}}
			</div>
		</div>
	</div>
</div>

<div id="panel-button-fixed" class="row hidden">
	<div class="col-md-12">
		<div class="panel panel-default">
			<div class="panel-body text-right">
				{{Form::button(trans('form.save'), array('class' => 'btn btn-primary', 'id' => 'submit'))}}
				@if(isset($customer->id))
				{{Form::button(trans('form.delete'), array('class' => 'btn btn-danger', 'id' => 'delete', 'data-id' => $customer->id))}}
				@endif
				{{Form::button(trans('form.back'), array('class' => 'btn btn-default', 'id' => 'cancel'))}}
			</div>
		</div>
	</div>
</div>

<div id="panel-error" class="row hidden">
	<div class="col-md-12">
		<div class="panel panel-default">
			<div id="error-content" class="panel-body"></div>
		</div>
	</div>
</div>

<div class='row'>
	<div class='col-md-12'>
		<div class="panel panel-default">
			<!--<header class="panel-heading">{{ trans('customer.create') }}</header>-->
			<div class="panel-body">
				{{Form::open(array('method' => 'post', 'class' => 'form-horizontal'))}}
					<div class="form-group">
						{{Form::label('name', trans('customer.name'), array('class' => 'col-sm-2 control-label'))}}
						<div class="col-sm-5">
							{{Form::text('name', (isset($customer->name))? $customer->name : null, array('class' => 'form-control', 'id' => 'name', 'placeholder' => trans('customer.name')))}}
						</div>
					</div>

					<div class="form-group">
						{{Form::label('address', trans('customer.address'), array('class' => 'col-sm-2 control-label'))}}
						<div class="col-sm-5">
							{{Form::text('address', (isset($customer->address))? $customer->address : null, array('class' => 'form-control', 'id' => 'address', 'placeholder' => trans('customer.address')))}}
						</div>
					</div>

					<div class="form-group">
						{{Form::label('address_print', trans('customer.address_print'), array('class' => 'col-sm-2 control-label'))}}
						<div class="col-sm-7">
							<div id="editor" style="height: 350px">
								{{isset($customer->address_print)? $customer->address_print : '';}}
							</div>
						</div>
					</div>

					<div class="form-group">
						{{Form::label('phone', trans('customer.phone'), array('class' => 'col-sm-2 control-label'))}}
						<div class="col-sm-5">
							{{Form::text('phone', (isset($customer->phone))? $customer->phone : null, array('class' => 'form-control', 'id' => 'phone', 'placeholder' => trans('customer.phone')))}}
						</div>
					</div>

					<div class="form-group">
						{{Form::label('order', trans('customer.order'), array('class' => 'col-sm-2 control-label'))}}
						<div class="col-sm-5">
							{{Form::text('order', (isset($customer->order))? $customer->order : 0, array('class' => 'form-control', 'id' => 'order', 'placeholder' => trans('customer.order')))}}
						</div>
					</div>

					<div class="form-group">
						{{Form::label('discount', trans('customer.discount'), array('class' => 'col-sm-2 control-label'))}}
						<div class="col-sm-5">
							{{Form::text('discount', (isset($customer->discount))? $customer->discount : 0, array('class' => 'form-control', 'id' => 'discount', 'placeholder' => trans('customer.discount')))}}
						</div>
					</div>

					{{Form::hidden('id', (isset($customer->id))? $customer->id : 0, array('id' => 'id'))}}
					{{Form::hidden('type', 'customer', array('id' => 'type'))}}
				{{Form::close()}}
			</div>
		</div>
	</div>
</div>

<script>
$(document).ready(function(){
	$("#submit").click(function(){
		var name          = $("#name").val();
		var address       = $("#address").val();
		var phone         = $("#phone").val();
		var order         = $("#order").val();
		var type          = $("#type").val();		
		var discount      = $("#discount").val();
		var address_print = $("div#editor").code();
		var id            = $("#id").val();
		var data          = {'name': name, 'address': address, 'phone': phone, 'order': order, 'type': type, 'discount': discount, 'address_print': address_print};

		if(id > 0){
			data = {'name': name, 'address': address, 'phone': phone, 'order': order, 'type': type, 'discount': discount, 'address_print': address_print, 'id': id};
		}

		deleteErrorMessages();

		$.ajax({
			url: "{{URL::to('/admin/customer/create')}}",
			type: "POST",
			beforeSend: ajaxLoading(),
			data: data,
			success: function(result){
				var rs = $.parseJSON(result);
				
				if(rs.error == true){
					appendErrorMessages(rs.message);
				}else{
					window.location = "{{URL::to('/admin/customer')}}";
				}

				ajaxLoadSuccess();
			}
		});
	});

	$("button#delete").click(function(){
		var ok = confirm("{{trans('customer.confirm_delete_this')}}");

		if(ok){
			var id = $(this).attr('data-id');

			$.ajax({
				url: '{{URL::to("admin/customer/delete")}}',
				type: 'POST',
				data: {'id': id},
				success: function(result){
					var url = "{{URL::to('/admin/customer/')}}";

					window.location = url;
				}
			});
		}
	});

	$("button#cancel").click(function(){
		window.history.back();
	});
});
</script>